package br.ufrj.dcc.smarthotel.activities;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import br.ufrj.dcc.smarthotel.R;
import br.ufrj.dcc.smarthotel.controls.ServicesMenuControl;
import br.ufrj.dcc.smarthotel.entities.Service;
import br.ufrj.dcc.smarthotel.listeners.ServicesMenuClickListener;

public class ServicesMenuActivity extends AppActivity {
	private ServicesMenuControl control;
	private ServicesMenuClickListener listener;
	
	private ListView listServices;
	
	private List<Service> servicesList;
	
	@Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.serviceslist);
        
        this.findAllViews();
        
        this.control = new ServicesMenuControl(this);
        this.listener = new ServicesMenuClickListener(this);
        
        this.control.loadServices();
    }

	private void findAllViews() {
		this.listServices = (ListView) this.findViewById(R.id.listServices);
	}

	public void setServicesList(List<Service> servicesList) {
		this.servicesList = servicesList;
		this.listServices.setAdapter(new ServicesListAdapter(this, R.id.listEvents, this.servicesList));
	}
	
	public List<Service> getServicesList() {
		return this.servicesList;
	}
	
	public class ServicesListAdapter extends ArrayAdapter<Service> {
		private List<Service> servicesList;
		
		public ServicesListAdapter(ServicesMenuActivity servicesMenuActivity, int listevents, List<Service> servicesList) {
			super(servicesMenuActivity, listevents, servicesList);
			this.servicesList = servicesList;
		}

		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			
	        if (v == null) {
	            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            
	            v = vi.inflate(R.layout.service_item, null);
	        }
	        
	        Service service = servicesList.get(position);
	        
	        if (service != null) {
                TextView txtServiceName = (TextView) v.findViewById(R.id.txtServiceName);
                
                txtServiceName.setText(service.getName());
	        }
	        return v;
		}
	}
}
